<template lang="pug">
div
  CascadeFilter(
      :showFilter="showFilter"
      :filterData="filterData"
      :cascade="cascade"
      @close="showFilter = false"
      @confirm="confirm"
      )

  FixedScroll
    div(slot="fixed")
      PageHeader(pageTitle="库存查询")
        div(slot="right" @click="showFilter = true") 筛选

      ScanSearch(
          placeholder="物料号"
          :initSearchValue="searchValue"
          @search="search"
          @scan="search"
          )

    StatTable.table(
        ref="table"
        slot="scroll"
        :headList="headList"
        :body="body"
        :isLoading="isLoading"
        @click-row="clickRow"
        )

    Pager(
        slot="fixed-bottom"
        :maxIndex="maxIndex"
        :curIndex="curIndex"
        @jump="jump"
        )

</template>

<script>
import api from "$api"
import util from "$util"

const PLANT = "2290"

export default {
  data() {
    // 设置级联
    let cascade = [
      ["StorgelocationCode", "WmNo", "ZoneNo"],
    ]

    // 配置筛选
    let filterData = [
      {
        k: "StorgelocationCode",
        kTitle: "实体仓库",
        tag: "dropdown",
        async getOptionList() {
          try {
            let data = await api.StockFilterGetReal({
              Plant: PLANT,
            })
            let newData = []
            for (let item of data) {
              if (item.ItemValue !== "") {
                newData.push({
                  id: item.ItemValue,
                  name: item.ItemText,
                })
              }
            }
            return newData
          } catch(err) {
            throw err
          }
        },
      },
      {
        k: "WmNo",
        kTitle: "逻辑仓库",
        tag: "dropdown",
        async getOptionList(req) {
          req.Plant = PLANT
          try {
            let data = await api.StockFilterGetLogical(req)
            let newData = []
            for (let item of data) {
              if (item.ItemValue !== "") {
                newData.push({
                  id: item.ItemValue,
                  name: item.ItemText,
                })
              }
            }
            return newData
          } catch(err) {
            throw err
          }
        },
      },
      {
        k: "ZoneNo",
        kTitle: "存储区",
        tag: "dropdown",
        async getOptionList(req) {
          req.Plant = PLANT
          try {
            let data = await api.StockFilterGetZone(req)
            let newData = []
            for (let item of data) {
              if (item.ItemValue !== "") {
                newData.push({
                  id: item.ItemValue,
                  name: item.ItemText,
                })
              }
            }
            return newData
          } catch(err) {
            throw err
          }
        },
      },
      {
        k: "Dloc",
        kTitle: "库位",
        tag: "input",
        scan: true,
      },
      {
        k: "PartNo",
        kTitle: "物料号",
        tag: "input",
      },
      {
        k: "PartCname",
        kTitle: "物料名称",
        tag: "input",
      },
      {
        k: "SupplierNum",
        kTitle: "厂家编号",
        tag: "input",
      },
      {
        k: "Keeper",
        kTitle: "保管员",
        tag: "input",
      },
      {
        k: "IsFrozenStocks",
        kTitle: "封存>0",
        tag: "switch",
        switchValue: false,
      },
    ]

    let bodyStyle = {
      color: "#1E88E5",
    }

    return {
      searchValue: "",
      isLoading: false,
      showFilter: false,
      filterData,
      cascade,

      loading: {
        confirm: false,
        search: false,
      },

      curIndex: 1,
      maxIndex: 1,
      lastReq: {
        type: "",
        param: null,
      },

      headList: [
        { field: "PartNo", title: "物料号", checked: true },
        { field: "PartCname", title: "物料名称", checked: true },
        { field: "StorgelocationCode", title: "实体仓库", checked: true },
        { field: "WmNo", title: "逻辑仓库", checked: true },
        { field: "SupplierNum", title: "厂家编号", checked: true },
        { field: "SupplierName", title: "厂家简称", checked: true },
        { field: "StocksNum", title: "总数量", checked: true },
        { field: "AvailbleNum", title: "可用数", checked: true },
        { field: "OccupancyNum", title: "占用数", checked: true, bodyStyle },
        { field: "Keeper", title: "保管员", checked: true },
        { field: "ZoneNo", title: "存储区", checked: true },
        { field: "Dloc", title: "库位", checked: true },
        { field: "PartType", title: "物料类型", checked: true },
        { field: "FrozenStocks", title: "不可用数", checked: true },
        { field: "PackageModel", title: "器具编号", checked: true },
        { field: "PackageQty", title: "收容数", checked: true },
      ],
      body: {
        list: [],
      },
    }
  },

  watch: {
    showFilter(v) {
      if (!v) {
        this.registerScanner()
      }
    },
  },

  methods: {
    // 筛选确定
    async confirm(filter, pageIndex = 1, pageSize = 20) {
      if (this.loading.confirm) {
        return
      }

      let param = { Plant: PLANT }
      for (let item of filter) {
        if (item.value === false) {
          item.value = 0
        } else if (item.value === true) {
          item.value = 1
        }
        param[item.key] = item.value
      }

      param.startRowIndex = (pageIndex - 1) * pageSize + 1
      param.maximumRows = pageSize
      this.lastReq.type = "confirm"
      this.lastReq.param = filter

      this.isLoading = true
      this.loading.confirm = true
      try {
        let data = await api.StockFilter(param)
        if (!data || !data.Result || data.Result.length === 0) {
          util.Toast("没有找到符合条件的记录")
          return
        }
        this.transform(data.Result)
        this.body.list = data.Result
        this.maxIndex = Math.ceil(data.total / pageSize)
        this.curIndex = pageIndex
      } catch(err) {
        this.$root.error(err)
      } finally {
        this.isLoading = false
        this.loading.confirm = false
      }
    },

    async search(value, pageIndex = 1, pageSize = 20) {
      if (this.loading.search) {
        return
      }
      if (value === "") {
        return
      }

      let param = {
        Plant: PLANT,
        PartNo: value,
      }

      param.startRowIndex = (pageIndex - 1) * pageSize + 1
      param.maximumRows = pageSize
      this.lastReq.type = "search"
      this.lastReq.param = value

      this.loading.search = true
      try {
        let data = await api.StockFilter(param)
        if (!data || !data.Result || data.Result.length === 0) {
          util.Toast("没有找到符合条件的记录")
          return
        }
        this.transform(data.Result)
        this.body.list = data.Result
        this.maxIndex = Math.ceil(data.total / pageSize)
        this.curIndex = pageIndex
      } catch(err) {
        this.$root.error(err)
      } finally {
        this.isLoading = false
        this.loading.search = false
      }
    },

    transform(list) {
      let enumList = api.Enum.StockMove.PartType.value
      for (let item of list) {
        item.PartType = enumList[item.PartType]
      }
    },

    clickRow(row) {
      let partType = 0
      let enumMap = api.Enum.StockMove.PartType.value
      for (let k in enumMap) {
        if (enumMap[k] === row.PartType) {
          partType = k
          break
        }
      }

      this.$router.push({
        path: "/stock/used",
        query: {
          Plant: row.Plant,
          StorgelocationCode: row.StorgelocationCode,
          WmNo: row.WmNo,
          ZoneNo: row.ZoneNo,
          SupplierNum: row.SupplierNum,
          PartNo: row.PartNo,
          PartType: partType,
        },
      })
    },

    jump(p) {
      if (this.lastReq.type === "confirm") {
        this.confirm(this.lastReq.param, p.curIndex)
      } else {
        this.search(this.lastReq.param, p.curIndex)
      }
    },

    registerScanner() {
      util.registerScanner((data) => {
        this.searchValue = data
        this.search(data)
      })
    },
  },

  created() {
    this.registerScanner()
  },

}
</script>

<style lang="stylus" scoped>

.table
  scan-table()

</style>
